package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.stream.IntStream;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ChatConfigMapper;
import com.ruoyi.system.domain.ChatConfig;
import com.ruoyi.system.service.IChatConfigService;

/**
 * 聊天记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-12
 */
@Service
public class ChatConfigServiceImpl implements IChatConfigService 
{
    @Autowired
    private ChatConfigMapper chatConfigMapper;

    /**
     * 查询聊天记录
     * 
     * @param id 聊天记录主键
     * @return 聊天记录
     */
    @Override
    public ChatConfig selectChatConfigById(Long id)
    {
        return chatConfigMapper.selectChatConfigById(id);
    }

    /**
     * 查询聊天记录列表
     * 
     * @param chatConfig 聊天记录
     * @return 聊天记录
     */
    @Override
    public List<ChatConfig> selectChatConfigList(ChatConfig chatConfig)
    {
        Long userId = SecurityUtils.getUserId();
        if (!SecurityUtils.isAdmin(userId)) {
            chatConfig.setCreateById(userId);
        }
        return chatConfigMapper.selectChatConfigList(chatConfig);
    }

    /**
     * 新增聊天记录
     * 
     * @param chatConfig 聊天记录
     * @return 结果
     */
    @Override
    public int insertChatConfig(ChatConfig chatConfig)
    {
        Random random = new Random();
        IntStream ints = random.ints(10000, 999999);
        int uniqueId = ints.findFirst().getAsInt();
        chatConfig.setUniqueId(uniqueId);
        chatConfig.setCreateById(SecurityUtils.getUserId());
        chatConfig.setCreateTime(new Date());
        chatConfig.setUpdateTime(new Date());
        chatConfig.setCreateBy(SecurityUtils.getUsername());
        chatConfig.setUpdateBy(SecurityUtils.getUsername());
        return chatConfigMapper.insertChatConfig(chatConfig);
    }

    /**
     * 修改聊天记录
     * 
     * @param chatConfig 聊天记录
     * @return 结果
     */
    @Override
    public int updateChatConfig(ChatConfig chatConfig)
    {
        chatConfig.setUpdateTime(new Date());
        chatConfig.setUpdateBy(SecurityUtils.getUsername());
        return chatConfigMapper.updateChatConfig(chatConfig);
    }

    /**
     * 批量删除聊天记录
     * 
     * @param ids 需要删除的聊天记录主键
     * @return 结果
     */
    @Override
    public int deleteChatConfigByIds(Long[] ids)
    {
        return chatConfigMapper.deleteChatConfigByIds(ids);
    }

    /**
     * 删除聊天记录信息
     * 
     * @param id 聊天记录主键
     * @return 结果
     */
    @Override
    public int deleteChatConfigById(Long id)
    {
        return chatConfigMapper.deleteChatConfigById(id);
    }

    @Override
    public ChatConfig selectChatConfigByUniqueId(Integer unique_id) {
        return chatConfigMapper.selectChatConfigByUniqueId(unique_id);
    }
}
